#------------------------------------------------------------------------------- # Name: A453 Programming Project Sept 2014 # CA Material 2 - Task 2 # Purpose: Simple Maths quiz # # Author: Andrew Baker # # Created: 24/02/2015 # Copyright: (c) Andrew Baker 2015 # Licence: #------------------------------------------------------------------------------- import random #---------------------------------------- def getclass(): classname="" found='No' prompt="What class are you in?" while found=='No': classname=input(prompt) if any(classname in s for s in classes): found='Yes' else: prompt="Try again. What class are you in?" return classname #---------------------------------------- def getname(): name=input("What's your name?") return name #---------------------------------------- def quiz(low,high): score=0 for n in range(10): firstnum=random.randrange(low,high+1) secondnum=random.randrange(low,high+1) operand=random.choice(operands) #This is a string version of the question being asked ques= str(firstnum)+" " + operand + " "+str(secondnum) ans=eval(ques) # eval, evaluates a string as a numeric expresion prompt="What is: " + ques + " ?" #Makes a string to be used in the Input dialogue box response=int(input(prompt)) # Get the answer from the user # Right or wrong? Deal with it! if response==ans: print("Well done") score +=1 else: print("Incorrect. {} = {} ".format(ques,ans)) return score #---------------------------------------- def showscore(score): print ("{} has scored {}".format(name,score)) #---------------------------------------- def writetofile(): writedata=name+":"+str(score)+"\n" filename=classname+".txt" file = open(filename,"a+") file.write(writedata) file.close() #---------------------------------------- # Set the program's parameters operands=["+","-","*"] classes=["4BA","4TC","4XY"] # Set the range of the random numbers low=1 #int(input("What's the lowest number?")) high=10 #int(input("What's the highest number?")) #Here's the quiz program classname=getclass() name=getname() score=quiz(low,high) showscore(score) writetofile()